"""
Case Type   : 资源池化系统视图
Case Name   : 默认配置下，构造failover操作，查看系统视图dbe_perf.track_memory_context
Create At   : 2025/2
Owner       : haomeng
Description :
    1、主节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    2、备节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    3、mv掉dn目录，kill掉dn进程,查看集群状态
    4、新主节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    5、新备节点查看系统表dbe_perf.track_memory_context中DMSDrcContext字段是否存在
    6、恢复初始环境
Expect      :
    1、无相关字段及信息
    2、无相关字段及信息
    3、原主异常，集群内产生新主
    4、无相关字段及信息
    5、无相关字段及信息
    6、环境恢复成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta2_sh = CommonSH('Standby2DbUser')
        self.user_node = Node(node='PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()

        self.sys_view1 = 'dbe_perf.track_memory_context'
        self.sys_view2 = 'dbe_perf.track_memory_context_detail()'

    def test_ss_system_view(self):
        text = '-----step1:主节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        self.log.info('---设置需要统计的内存申请详细信息的内存上下文---')
        sql1 = f"select * from {self.sys_view1}('DMSMemContext');"
        self.log.info(sql1)
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertIn('t', res1.splitlines()[-2].strip(), '内存上下文设置失败')

        sql2 = f"select * from {self.sys_view2};"
        self.log.info(sql2)
        res2 = self.pri_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res2)
        self.assertIn('(0 rows)', res2, '存在异常信息')

        text = '-----step2:备节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res3 = self.sta1_sh.execut_db_sql(sql1)
        self.log.info(res3)
        self.assertIn('t', res3.splitlines()[-2].strip(), '内存上下文设置失败')

        res4 = self.sta1_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res4)
        self.assertIn('(0 rows)', res4, '存在异常信息')

        text = '-----step3:mv掉dn目录，kill掉dn进程,查看集群状态  expect:原主异常，集群内产生新主-----'
        self.log.info(text)

        pid = self.com.get_pid(self.user_node, 'bin/gaussdb')
        self.log.info(pid)

        mv = self.user_node.sh(f'mv {macro.DB_INSTANCE_PATH} {macro.DB_INSTANCE_PATH}_bak').result()
        self.log.info(mv)

        kill = self.user_node.sh(f'kill -9 {pid}').result()
        self.log.info(kill)
        time.sleep(60)
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertNotIn('Normal', status.splitlines()[-1].split('|')[0], '执行失败')
        self.assertTrue('Primary Normal' in status.splitlines()[-1].split('|')[1]
                        or 'Primary Normal' in status.splitlines()[-1].split('|')[-1], '集群状态异常')

        text = '-----step4:新主节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res5 = self.sta1_sh.execut_db_sql(sql1)
        self.log.info(res4)
        self.assertIn('t', res5.splitlines()[-2].strip(), '内存上下文设置失败')

        res6 = self.sta1_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res6)
        self.assertIn('(0 rows)', res6, '存在异常信息')

        text = '-----step5:新备节点查看系统表dbe_perf.track_memory_context中DMSMemContext字段是否存在  ' \
               'expect:无相关字段及信息-----'
        self.log.info(text)

        res7 = self.sta2_sh.execut_db_sql(sql1)
        self.log.info(res7)
        self.assertIn('t', res7.splitlines()[-2].strip(), '内存上下文设置失败')

        res8 = self.sta2_sh.execut_db_sql(sql2, dbname='postgres')
        self.log.info(res8)
        self.assertIn('(0 rows)', res8, '存在异常信息')

    def tearDown(self):
        text = '-----step6:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        mv_bak = self.user_node.sh(f'mv {macro.DB_INSTANCE_PATH}_bak {macro.DB_INSTANCE_PATH};'
                                   f'ls {macro.DB_INSTANCE_PATH}').result()
        self.log.info(mv_bak)

        start = self.pri_sh.exec_cm_ctl('start')
        self.log.info(start)

        restore = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(restore)
        self.assertIn(self.constant.cm_switchover_success_msg, restore, '执行失败')

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('Primary Normal', status.splitlines()[-1].split('|')[0],
                      '恢复集群初始状态失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
